using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using System.Globalization;

namespace IWCommunity.Blogs.WebControls
{
    public enum MELEntityType
    {
        None = 0,
        Web = 1,
        List = 2,
        ListItem = 3,
        Site = 4,
        Application = 5,
        Farm = 6
    }

    public class MasterListItem
    {
        #region Class Level Variables

        private Guid _SiteID;
        private SPUser _User;
        private string _UserDisplayName = string.Empty;
        
        private Guid _EntityID;
        private MELEntityType _EntityTypeID;
        private string _EntityURL = string.Empty;
        private bool _EntityActive;
        private string _EntityDisplayName = string.Empty;
        private SPContentType _EntityContentType;
        
        private Guid _WebID;
        private Guid _ParentWebID;
      
        private string _ParentEntityDisplayName = string.Empty;
        private Guid _ParentEntityID;
        private MELEntityType _ParentEntityTypeID;
        private string _ParentEntityURL = string.Empty;
   
        #endregion

        #region Constructors

        public MasterListItem()
        {
        }

        public MasterListItem(Guid SiteID) : this()
        {
            _SiteID = SiteID;
        }

        #endregion

        #region Properties

        public Guid SiteID
        {
            get
            {
                return _SiteID;
            }
            set
            {
                _SiteID = value;
            }
        }

        public Guid EntityID
        {
            get
            {
                return _EntityID;
            }
            set
            {
                _EntityID = value;
            }
        }

        public MELEntityType EntityTypeID
        {
            get
            {
                return _EntityTypeID;
            }
            set
            {
                _EntityTypeID = value;
            }
        }

        public MELEntityType ParentEntityTypeID
        {
            get
            {
                return _ParentEntityTypeID;
            }
            set
            {
                _ParentEntityTypeID = value;
            }
        }

        public Guid WebID
        {
            get
            {
                return _WebID;
            }
            set
            {
                _WebID = value;
            }
        }

        public Guid ParentWebID
        {
            get
            {
                return _ParentWebID;
            }
            set
            {
                _ParentWebID = value;
            }
        }

        public SPUser User
        {
            get
            {
                return _User;
            }
            set
            {
                _User = value;
            }
        }

        public int UserID
        {
            get
            {
                if (_User != null)
                {
                    return _User.ID;
                }
                else
                {
                    return 0;
                }
            }
        }

        public string UserDisplayName
        {
            get
            {
                return _UserDisplayName;
            }
            set
            {
                _UserDisplayName = value;
            }
        }

        public string UserLoginName
        {
            get
            {
                if (_User != null)
                {
                    return _User.LoginName;
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        public string EntityURL
        {
            get
            {
                return _EntityURL;
            }
            set
            {
                _EntityURL = value;
            }
        }

        public string EntityDisplayName
        {
            get
            {
                return _EntityDisplayName;
            }
            set
            {
                _EntityDisplayName = value;
            }
        }

        public string ParentEntityDisplayName
        {
            get
            {
                return _ParentEntityDisplayName;
            }
            set
            {
                _ParentEntityDisplayName = value;
            }
        }

        public Guid ParentEntityID
        {
            get
            {
                return _ParentEntityID;
            }
            set
            {
                _ParentEntityID = value;
            }
        }

        public bool EntityActive
        {
            get
            {
                return _EntityActive;
            }
            set
            {
                _EntityActive = value;
            }
        }

        public string ParentEntityURL
        {
            get
            {
                return _ParentEntityURL;
            }
            set
            {
                _ParentEntityURL = value;
            }
        }

        public SPContentType EntityContentType
        {
            get
            {
                return _EntityContentType;
            }
            set
            {
                _EntityContentType = value;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// An SPUser field contains a string of the form ID;#User_Display_Name, where ID is the member ID of the associated user.
        //  reference: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/spptsdk/html/tscSPListItem_SV01013571.asp
        /// </summary>
        public static string FormatUserName(string UserInfo)
        {
            string userName = string.Empty;

            if (!string.IsNullOrEmpty(UserInfo))
            {
                int index = UserInfo.IndexOf('#');
                userName = UserInfo.Substring(index + 1);
            }

            return userName;
        }

        public static int FormatUserID(string UserInfo)
        {
            int userID = 0;
            string userIdString;

            if (!string.IsNullOrEmpty(UserInfo))
            {
                userIdString = UserInfo.Substring(0, UserInfo.IndexOf(';'));
                userID = int.Parse(userIdString, CultureInfo.InvariantCulture);
            }

            return userID;
        }

        /// <summary>
        /// We're storing EntityURL as a SharePoint url type in the Master Event List
        /// and SharePoint stores this a comma delimited string with 2 urls - so
        /// we just need to get the first one
        /// </summary>
        public static HyperLink FormatHyperlink(string Url, string DisplayName)
        {
            HyperLink hyperLink1 = new HyperLink();
            string[] entityUrl;
            string navigateUrl = string.Empty;

            if (!string.IsNullOrEmpty(Url))
            {
                //We're storing EntityURL as a SharePoint url type in the Master Event List
                //and SharePoint stores this a comma delimited string with 2 urls - so
                //we just need to get the first one
                if (!string.IsNullOrEmpty(DisplayName))
                {
                    entityUrl = Url.Split(new char[] { ',' });
                    navigateUrl = entityUrl[0];
                }

                hyperLink1.NavigateUrl = navigateUrl;
                hyperLink1.Text = DisplayName;
            }

            return hyperLink1;
        }

        #endregion
    }

    public struct MasterListFields
    {
        public const string EVENTID = "EventID";
        public const string ENTITYID = "EntityID";
        public const string ENTITYTYPEID = "EntityTypeID";
        public const string PARENTENTITYTYPEID = "ParentEntityTypeID";
        public const string EVENTTYPEID = "EventTypeID";
        public const string WEBID = "WebID";
        public const string PARENTWEBID = "ParentWebID";
        public const string SITEID = "SiteID";
        public const string EVENTCREATED = "EventCreated";
        public const string CREATED = "Created";
        public const string MODIFIED = "Modified";
        public const string ENTITYURL = "EntityURL";
        public const string ENTITYDISPLAYNAME = "EntityDisplayName";
        public const string PARENTENTITYDISPLAYNAME = "ParentEntityDisplayName";
        public const string PARENTENTITYID = "ParentEntityID";
        public const string ENTITYACTIVE = "EntityActive";
        public const string PARENTENTITYURL = "ParentEntityURL";
        public const string ENTITYCONTENTTYPEID = "EntityContentTypeID";
        public const string ENTITYRATING = "EntityRating";
        public const string ENTITYMAXRATING = "EntityMaxRating";
        public const string ENTITYADJRATING = "EntityAdjRating";
        public const string USERINFO = "UserInfo";
        public const string USERDISPLAYNAME = "UserDisplayName";
        public const string ENTITYCOUNT = "EntityCount";
        public const string ENTITYDESCRIPTION = "EntityDescription";
    }
}
